This example uses the MoveFirst, MoveLast, MoveNext, and MovePrevious methods to move the record pointer of a Recordset based on the supplied command. The MoveAny procedure is required for this procedure to run.
Use the following example in an Active Server Page (ASP). To view this fully functional example, you must have the data source AdvWorks.mdb (installed with the SDK) located at C:\mssdk\samples\dataaccess\rds. This is a Microsoft Access database file.
Use Find to locate the file Adovbs.inc and place it in the directory you plan to use. Cut and paste the following code into Notepad or another text editor, and save it as MoveOne.asp. You can view the result in any browser.
Try moving beyond the upper or lower limits of the recordset to see error handling work.
<%@ Language=VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML><HEAD>
<TITLE>ADO MoveNext, MovePrevious, MoveLast, MoveFirst Methods</TITLE>
<STYLE>
<!--
BODY {
font-family: "MS SANS SERIF",sans-serif;
}
.thead {
background-color: #008080;
font-family: 'Arial Narrow','Arial',sans-serif;
font-size: x-small;
color: white;
}
.tbody {
text-align: center;
background-color: #f7efde;
font-family: 'Arial Narrow','Arial',sans-serif;
font-size: x-small;
}
.tmsg {
color: red;
text-align: center;
}
-->
</STYLE>
</HEAD>
<BODY>
<H3>ADO Methods<BR>MoveNext, MovePrevious, MoveLast, MoveFirst</H3>
<!-- Create Connection and Recordset Objects on Server -->
<%
src = "c:\mssdk\samples\dataaccess\rds\advworks.mdb"
sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & src
'Create and Open Connection Object
Set OBJdbConn = Server.CreateObject("ADODB.Connection")
OBJdbConn.Open sConnStr
'Create and Open Recordset Object
Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
RsCustomerList.ActiveConnection = OBJdbConn
RsCustomerList.CursorType = adOpenKeyset
RsCustomerList.CursorLocation = adUseClient
RsCustomerList.LockType = adLockOptimistic
RsCustomerList.Source = "Customers"
RsCustomerList.Open
RsCustomerList.MoveFirst
If Not IsEmpty(Request.Form("MoveAction")) Then
strAction = Request.Form("MoveAction")
varPosition = Request.Form("Position")
RsCustomerList.AbsolutePosition = varPosition
Select Case strAction
Case "MoveNext"
RsCustomerList.MoveNext
If RsCustomerList.EOF Then
RsCustomerList.MoveLast
strMessage = "Can't move beyond the last record."
End If
Case "MovePrev"
RsCustomerList.MovePrevious
If RsCustomerList.BOF Then
RsCustomerList.MoveFirst
strMessage = "Can't move beyond the first record."
End If
Case "MoveLast"
RsCustomerList.MoveLast
Case "MoveFirst"
RsCustomerList.MoveFirst
End Select
End If
%>
<H3>Current Record Number is <BR>
<!-- Display Current Record Number and Recordset Size -->
<%=RsCustomerList.AbsolutePosition%> of <%=RsCustomerList.RecordCount%></H3>
<HR>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<!-- BEGIN column header row for Customer Table-->
<TR CLASS=thead>
<TD>Company Name</TD>
<TD>Contact Name</TD>
<TD>Phone Number</TD>
<TD>City</TD>
<TD>State/Province</TD>
</TR>
<!--Display ADO Data from Customer Table-->
<TR CLASS=tbody>
<TD> <%= RSCustomerList("CompanyName")%> </TD>
<TD> <%= RScustomerList("ContactLastName") & ", " %>
<%= RScustomerList("ContactFirstName") %> </TD>
<TD> <%= RScustomerList("PhoneNumber")%> </TD>
<TD> <%= RScustomerList("City")%> </TD>
<TD> <%= RScustomerList("StateOrProvince")%> </TD>
</TR>
<TR CLASS=tmsg>
<TD COLSPAN=5><%=strMessage%></TD>
</TR>
</TABLE>
<HR>
<Input Type=Button Name=cmdDown Value="< ">
<Input Type=Button Name=cmdUp Value=" >">
<BR>
<Input Type=Button Name=cmdFirst Value="First Record">
<Input Type=Button Name=cmdLast Value="Last Record">
<H5>Click Direction Arrows to Use MovePrevious or MoveNext</H5>
<!-- Use Hidden Form Fields to send values to Server -->
<Form Method=Post
Action="<%=Request.ServerVariables("SCRIPT_NAME")%>"
Name=Form>
<Input Type="Hidden" Size="4" Name="MoveAction" Value="Move">
<Input Type="Hidden" Size="4" Name="Position" Value="<%= RsCustomerList.AbsolutePosition %>">
</Form>
<HR>
</BODY>
<Script Language = "VBScript">
Sub cmdDown_OnClick
'Set Values in Form Input Boxes and Submit Form
Document.form.MoveAction.Value = "MovePrev"
Document.Form.Submit
End Sub
Sub cmdUp_OnClick
Document.form.MoveAction.Value = "MoveNext"
Document.Form.Submit
End Sub
Sub cmdFirst_OnClick
Document.form.MoveAction.Value = "MoveFirst"
Document.Form.Submit
End Sub
Sub cmdLast_OnClick
Document.form.MoveAction.Value = "MoveLast"
Document.Form.Submit
End Sub
</Script>